package com.xgame.server.dbtool;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.xgame.server.db.mapper.HeroMapper;
import com.xgame.server.db.model.Hero;
import com.xgame.server.netty.proto.Game;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Component
public class HeroCsTool {
    @Resource
    private HeroMapper heroMapper;

    //插入经验副本
    public Integer insertHero(Game.HeroDataCS data) throws Exception {
        //1.删除本玩家的所有
        deleteHero(data.getAccount());

        //2.插入
        List<Game.HeroOneCS> items = data.getHerosList();
        int length = items.size();
        Integer count = new Integer(0);
        for (int i = 0; i < length; i++) {
            Game.HeroOneCS one = items.get(i);
            Hero map = new Hero();
            map.setAccount(data.getAccount());
            map.setPlayerId(data.getPlayerID());
            map.setIsPlayer(one.getIsPlayer() ? 1 : 0);
            map.setIsBattle(one.getIsBattle() ? 1 : 0);
            map.setHeroId(one.getId());
            map.setHeroLevel(one.getLevel());
            map.setHeroExp(new Long(one.getExp()));
            map.setGongExp(one.getGongExp());
            map.setHeroStation(one.getStation());
            map.setHeroStar(one.getStar());

            String lst = "";
            List<Game.AttrMedicineCs> complst = one.getMedicineLstList();

            HashMap<Integer, Integer> mation = new HashMap<Integer, Integer>();

            int nlength = complst.size();
            for (int j = 0; j < nlength; j++) {
                if (mation.containsKey(complst.get(j).getItemId())) {
                    //包含了该药品id，则数目进行相加
                    Integer xcount = mation.get(complst.get(j).getItemId());
                    xcount += complst.get(j).getNum();
                    mation.put(complst.get(j).getItemId(), xcount);
                } else {
                    //不含有该药品，则进行创建
                    mation.put(complst.get(j).getItemId(), complst.get(j).getNum());
                }
            }

            Integer key = null;
            Integer integ = null;
            Iterator iter = mation.entrySet().iterator();
            while (iter.hasNext()) {
                Map.Entry entry = (Map.Entry) iter.next();

                key = (Integer) entry.getKey();
                integ = (Integer) entry.getValue();

                lst = lst + key.intValue() + ";" + integ.intValue() + "^";
            }

            if (lst.contains("^")) {
                lst = lst.substring(0, lst.length() - 1);
            }

            map.setMedicineLst(lst);
            count += heroMapper.insert(map);
        }

        return count;
    }

    //删除记录
    public Integer deleteHero(String account) throws Exception {
        EntityWrapper<Hero> userWrapper = new EntityWrapper<>();
        userWrapper.eq("account", account);

        return heroMapper.delete(userWrapper);
    }

    //查询记录
    public List<Hero> selectHero(String account) throws Exception {
        EntityWrapper<Hero> userWrapper = new EntityWrapper<>();
        userWrapper.eq("account", account);

        return heroMapper.selectList(userWrapper);
    }
}
